<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library(array('form_validation', 'session'));
		$this->load->database();	
	}
	
	public function index()
	{
		$config['view'] = 'admin/admin_view';
		$this->load->view('admin/admin_global_view', $config);
	}
	
	public function login()
	{
		$this->load->view('admin/admin_login_view');
	}
	
	public function post_type($id = NULL)
	{
		// auth
		/*
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user','refresh');
		}*/
		
		$this->form_validation->set_rules('name', '分类名称', 'required');		
		$this->form_validation->set_rules('order', 'order', 'required');
//		$this->form_validation->set_rules('url', 'url', 'required|prep_url');
		
		// db record
		$post_type_records = $this->db->get('post_type')->result();
		
//		$post_type = new stdClass();
		$post_type_name = '';
		$post_type_order = ''; 
		if($id != NULL)
		{
			$post_type = $this->db->get_where('post_type', array('id' => $id), 1)->row();
			if(!$post_type)
				show_404();
			$post_type_name = $post_type->name;
			$post_type_order = $post_type->order;
		}
		if($id != NULL)
		{
			$config['post_type_title'] = '编辑分类';
		}
		else
		{
			$config['post_type_title'] = '增加分类';
		}	
			
		$config['view'] = 'admin/admin_post_type_view';
		$config['id'] = $id;
		$config['post_type_records'] = $post_type_records;
		$config['post_type_name'] = $post_type_name;
		$config['post_type_order'] = $post_type_order;
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/admin_global_view', $config);
		}
		else
		{			
			if($this->input->post('id') == NULL)
				$this->post_type_add();
			else
				$this->post_type_edit($this->input->post('id'));	
		}	
		
			
	}
	
	function post_type_add()
	{
		$data = array(
			'name' => $this->input->post('name'),
			'order' => $this->input->post('order'),
		);
		$this->db->insert('post_type', $data);	
		$this->session->set_flashdata('success_message', '增加成功');
		
		redirect('admin/post_type', 'refresh');		
	}
	
	function post_type_edit($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$data = array(
			'name' => $this->input->post('name'),
			'order' => $this->input->post('order'),
		);
		$this->db->where('id', $id);
		$this->db->update('post_type', $data);
		
		$this->session->set_flashdata('update_message', '编辑成功');
		
		redirect('admin/post_type/' . $id, 'refresh');		
	}
	
	function post_type_delete($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$this->db->where('id', $id);
		$this->db->delete('post_type');
		
		$this->load->library('session');
		$this->session->set_flashdata('success_message', '删除成功');
		
		$this->load->helper('url');
		redirect('admin/post_type', 'refresh');
	}
	
	function post_type_disable($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$data = array(
			'status' => 0,
		);
		
		$this->db->where('id', $id);
		$this->db->update('post_type', $data);
		
		$this->load->library('session');
		$this->session->set_flashdata('success_message', '设置成功');
		
		$this->load->helper('url');
		redirect('admin/post_type', 'refresh');
	}
	function post_type_enable($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$data = array(
			'status' => 1,
		);
		
		$this->db->where('id', $id);
		$this->db->update('post_type', $data);
		
		$this->load->library('session');
		$this->session->set_flashdata('success_message', '设置成功');
		
		$this->load->helper('url');
		redirect('admin/post_type', 'refresh');
	}
	
	public function post_list($id = NULL)
	{
		// db record
		$post_records = $this->db->get('post')->result();
		$post_type_rs = $this->db->get('post_type');
		$post_type_records = array();
		foreach($post_type_rs->result() as $post_type)
		{
			$post_type_records[$post_type->id] = $post_type->name;
		}
//		print_r($post_type_records);
		$config['view'] = 'admin/admin_post_list_view';
		$config['post_records'] = $post_records;
		$config['post_type_records'] = $post_type_records;
		$this->load->view('admin/admin_global_view', $config);
	
	}
	public function post($id = NULL)
	{
		// auth
		/*
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user','refresh');
		}*/
		
		$post_records = new stdClass();
		$post_records->name = '';
		$post_records->content = '';
		$post_records->typeid = '';
		$post_records->order = '';
		$post_records->status = '';
		$post_records->keywords = '';
		if($id != NULL)
		{
			$post_records = $this->db->get_where('post', array('id'=>$id), 1)->row();
			if(!$post_records)
				show_404();
		}
		
		$post_type_records = $this->db->get('post_type')->result();
		
		$this->form_validation->set_rules('name', '文章名称', 'required');		
		$this->form_validation->set_rules('content', '文章内容', 'required');
		$this->form_validation->set_rules('typeid', 'typeid', 'required');
		$this->form_validation->set_rules('order', 'order', 'required');
		$this->form_validation->set_rules('status', 'status', 'required');
		$this->form_validation->set_rules('keywords');
//		$this->form_validation->set_rules('url', 'url', 'required|prep_url');
		
		
		if($id != NULL)
		{
			$config['post_title'] = '编辑文章';
		}
		else
		{
			$config['post_title'] = '增加文章';
		}	
			
		$config['view'] = 'admin/admin_post_view';
		$config['post_records'] = $post_records;
		$config['post_type_records'] = $post_type_records;
		$config['id'] = $id;
	
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/admin_global_view', $config);
		}
		else
		{			
			if($this->input->post('id') == NULL)
				$this->post_add();
			else
				$this->post_edit($this->input->post('id'));	
		}	
		
			
	}
	
	function post_add()
	{
		$data = array(
			'name'=>$this->input->post('name'),
			'content'=>$this->input->post('content'),
			'typeid'=>$this->input->post('typeid'),
			'order'=>$this->input->post('order'),
			'status'=>$this->input->post('status'),
			'keywords'=>$this->input->post('keywords'),
			'create'=>date('Y-m-d H:i:s'),
			'update'=>date('Y-m-d H:i:s'),
		);
		$this->db->insert('post', $data);	
		$this->session->set_flashdata('success_message', '增加成功');
		
		redirect('admin/post_list', 'refresh');		
	}
	
	function post_edit($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$data = array(
			'name'=>$this->input->post('name'),
			'content'=>$this->input->post('content'),
			'typeid'=>$this->input->post('typeid'),
			'order'=>$this->input->post('order'),
			'status'=>$this->input->post('status'),
			'keywords'=>$this->input->post('keywords'),
			'update'=>date('Y-m-d H:i:s'),
		);
		$this->db->where('id', $id);
		$this->db->update('post', $data);
		
		$this->session->set_flashdata('update_message', '编辑成功');
		
		redirect('admin/post/' . $id, 'refresh');		
	}
	
	function post_delete($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$this->db->where('id', $id);
		$this->db->delete('post');
		
		$this->load->library('session');
		$this->session->set_flashdata('success_message', '删除成功');
		
		$this->load->helper('url');
		redirect('admin/post_list', 'refresh');
	}
	
	function post_disable($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$data = array(
			'status' => 0,
		);
		$this->db->where('id', $id);
		$this->db->update('post', $data);
		
		$this->load->library('session');
		$this->session->set_flashdata('success_message', '设置成功');
		
		$this->load->helper('url');
		redirect('admin/post_list', 'refresh');
	}
	function post_enable($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$data = array(
			'status' => 1,
		);
		$this->db->where('id', $id);
		$this->db->update('post', $data);
		
		$this->load->library('session');
		$this->session->set_flashdata('success_message', '设置成功');
		
		$this->load->helper('url');
		redirect('admin/post_list', 'refresh');
	}
	
	function global_options()
	{
		$config['view'] = 'admin/admin_global_options_view';
		$this->load->view('admin/admin_global_view', $config);
	}		
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */